package tw.arduino.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import tw.arduino.module.Config;
import tw.arduino.module.PublicObj;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Label;
import com.swtdesigner.SWTResourceManager;
/**
 * @ArduinoTW Command Center
 * @author XLINX x
 * @since 2010.08.08
 * @see http://arduino.tw
 */

public class MainFrame {

	protected Shell shlArduinotwCommandCenter;
	public static Display display;
	public static PCSettingComposite pcSettingC;
	public static TerminalComposite chC;
	public static ThreeDurino threeDurino;
	public static Label statusLabel;
	private Label timeLabel;
	public static Label lblRxTxbytes;
	DateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
	public TimerTask statusTimer = new TimerTask() {
		public void run() {
			display.syncExec(new Runnable() {
				public void run() {
					String t = dateFormat.format(new Date());
					switch (new java.util.Date().getSeconds() % 6) {
					case 0:
						timeLabel.setText(t + " |+---|");
						break;
					case 1:
						timeLabel.setText(t + " |-+--|");
						break;
					case 2:
						timeLabel.setText(t + " |--+-|");
						break;
					case 3:
						timeLabel.setText(t + " |---+|");
						break;
					case 4:
						timeLabel.setText(t + " |--+-|");
						break;
					case 5:
						timeLabel.setText(t + " |-+--|");
						break;
					}
				}
			});
		}
	};
	private SWTBrowserComposite composite_3;
	private Composite composite_4;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.err.println("[ArduinoTW CC][v1][]");
			Config.getInstance();
			PublicObj.getInstance();
			new loadingFrame();
			MainFrame window = new MainFrame();
			window.open();		

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void updateSomething(final int key,final String val) {
		display.syncExec(new Runnable() {
			public void run() {
				switch (key) {
				case 0:
				}
			}
		});

	}

	/**
	 * Open the window.
	 */
	public void open() {
		display = Display.getDefault();		
		createContents();
		new java.util.Timer().scheduleAtFixedRate(statusTimer, 0, 1000);
		shlArduinotwCommandCenter.open();
		shlArduinotwCommandCenter.layout();
		while (!shlArduinotwCommandCenter.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlArduinotwCommandCenter = new Shell();
		shlArduinotwCommandCenter.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/ArduinoTWLOGO.png"));
		shlArduinotwCommandCenter.setText("ArduinoTW Command Center - Ver1.0beta");
		GridLayout gl_shlArduinotwCommandCenter = new GridLayout(1, false);
		gl_shlArduinotwCommandCenter.horizontalSpacing = 0;
		gl_shlArduinotwCommandCenter.verticalSpacing = 0;
		gl_shlArduinotwCommandCenter.marginWidth = 0;
		gl_shlArduinotwCommandCenter.marginHeight = 0;
		shlArduinotwCommandCenter.setLayout(gl_shlArduinotwCommandCenter);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		shlArduinotwCommandCenter.setSize(screenSize.width / 2, screenSize.height / 2);
		if (shlArduinotwCommandCenter.getSize().y > screenSize.height)
			shlArduinotwCommandCenter.getSize().y = screenSize.height;
		if (shlArduinotwCommandCenter.getSize().x > screenSize.width)
			shlArduinotwCommandCenter.getSize().x = screenSize.width;
		shlArduinotwCommandCenter.setLocation((screenSize.width - shlArduinotwCommandCenter.getSize().x) / 2, (screenSize.height - shlArduinotwCommandCenter.getSize().y) / 2);
		shlArduinotwCommandCenter.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent arg0) {
				System.exit(0);
			}
		});
		Composite composite = new Composite(shlArduinotwCommandCenter, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		composite.setBounds(0, 0, 64, 64);

		CTabFolder tabFolder = new CTabFolder(composite, SWT.BORDER);
		tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		tabFolder.setSimple(false);
		tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		CTabItem tbtmMonitor = new CTabItem(tabFolder, SWT.NONE);
		tbtmMonitor.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/gif/info_obj.gif"));
		tbtmMonitor.setText("PC-Setting");

		pcSettingC = new PCSettingComposite(tabFolder, SWT.NONE);
		tbtmMonitor.setControl(pcSettingC);
		pcSettingC.setLayout(new GridLayout(1, false));

		CTabItem tbtmRelay = new CTabItem(tabFolder, SWT.NONE);
		tbtmRelay.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/gif/debug_exc.gif"));
		tbtmRelay.setText("Monitor");

		chC = new TerminalComposite(tabFolder, SWT.NONE);
		tbtmRelay.setControl(chC);
		
		CTabItem tbtmTelnetserver = new CTabItem(tabFolder, SWT.NONE);
		tbtmTelnetserver.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/gif/console_view.gif"));
		tbtmTelnetserver.setText("SocketServer");
		
		composite_4 = new Composite(tabFolder, SWT.NONE);
		tbtmTelnetserver.setControl(composite_4);

		CTabItem tbtmConfig = new CTabItem(tabFolder, SWT.NONE);
		tbtmConfig.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/gif/variable_tab.gif"));
		tbtmConfig.setText("Config");

		ConfigComposite composite_2 = new ConfigComposite(tabFolder, SWT.NONE);
		tbtmConfig.setControl(composite_2);
		composite_2.setLayout(new GridLayout(1, false));
		
		CTabItem tbtmBrowser = new CTabItem(tabFolder, SWT.NONE);
		tbtmBrowser.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/gif/members.gif"));
		tbtmBrowser.setText("Browser");
		
		composite_3 = new SWTBrowserComposite(tabFolder, SWT.NONE);
		tbtmBrowser.setControl(composite_3);
		
		CTabItem tbtmdurino = new CTabItem(tabFolder, SWT.NONE);
		tbtmdurino.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/gif/brkp_grp.gif"));
		tbtmdurino.setText("3Durino");
		threeDurino=new ThreeDurino(tabFolder, SWT.NONE);
		tbtmdurino.setControl(threeDurino);

		Composite composite_1 = new Composite(shlArduinotwCommandCenter, SWT.NONE);
		composite_1.setLayout(new GridLayout(5, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		composite_1.setBounds(0, 0, 64, 64);

		statusLabel = new Label(composite_1, SWT.NONE);
		statusLabel.setAlignment(SWT.RIGHT);
		statusLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		statusLabel.setText("Arduino.TW Control Center by YS-Lin and ST-Liu");

		Label lblF = new Label(composite_1, SWT.SEPARATOR | SWT.VERTICAL);
		GridData gd_lblF = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblF.heightHint = 15;
		lblF.setLayoutData(gd_lblF);

		lblRxTxbytes = new Label(composite_1, SWT.NONE);
		lblRxTxbytes.setAlignment(SWT.CENTER);
		GridData gd_lblRxTxbytes = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblRxTxbytes.widthHint = 180;
		lblRxTxbytes.setLayoutData(gd_lblRxTxbytes);
		lblRxTxbytes.setText("Rx:"+PublicObj.numRx+" Tx:"+PublicObj.numTx+" (bytes)");

		Label label_4 = new Label(composite_1, SWT.SEPARATOR | SWT.VERTICAL);
		GridData gd_label_4 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_label_4.heightHint = 15;
		label_4.setLayoutData(gd_label_4);

		timeLabel = new Label(composite_1, SWT.NONE);
		timeLabel.setAlignment(SWT.CENTER);
		GridData gd_timeLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_timeLabel.widthHint = 100;
		timeLabel.setLayoutData(gd_timeLabel);
		timeLabel.setText("00:00:00");

	}
}
class loadingFrame {
	Shell shellX;

	Display displayX = Display.getDefault();

	ProgressBar progressBar;

	java.util.TimerTask timetimex;
	
//	public loadingFrame(){
//		MainFrame.display.syncExec(new Runnable() {
//			public void run() {
//				loadingFramex();
//			}
//		});
//	}
	public loadingFrame() {
		shellX = new Shell(SWT.RESIZE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		gridLayout.marginHeight = 0;
		gridLayout.horizontalSpacing = 1;
		shellX.setLayout(gridLayout);
		shellX.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
		shellX.setSize(502, 256);
		shellX.setText("ArduinoTW Command Center");
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		if (shellX.getSize().y > screenSize.height)
			shellX.getSize().y = screenSize.height;
		if (shellX.getSize().x > screenSize.width)
			shellX.getSize().x = screenSize.width;
		shellX.setLocation((screenSize.width - shellX.getSize().x) / 2, (screenSize.height - shellX.getSize().y) / 2);
		final Label label = new Label(shellX, SWT.NONE);
		final GridData gd_label = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_label.minimumWidth = 490;
		gd_label.minimumHeight = 230;
		gd_label.widthHint = 490;
		label.setLayoutData(gd_label);
		label.setImage(SWTResourceManager.getImage(MainFrame.class, "/images/ArduinoTWCC_initFrame" + new Integer((int) (Math.random() * 4) ).toString() + ".png"));
		// if(InetAddress.getByName("www.google.com")!=null)
		timetimex = new TimerTask() {
			public void run() {
				displayX.syncExec(new Runnable() {
					public void run() {
						if (progressBar.getSelection() <= 99) {
							progressBar.setSelection(progressBar.getSelection() + 1);
							// igas.sound.SoundControlCenter.playSound("WELCOME",igas.
							// );
						} else {
							displayX.dispose();
							shellX.dispose();
							timetimex.cancel();
						}

					}
				});
			}
		};

		progressBar = new ProgressBar(shellX, SWT.SMOOTH);
		final GridData ddd = new GridData(SWT.FILL, SWT.BOTTOM, false, false);
		progressBar.setLayoutData(ddd);
		progressBar.setMaximum(100);

		new java.util.Timer().scheduleAtFixedRate(timetimex, 0, 10);
		shellX.open();
		shellX.layout();

		while (!shellX.isDisposed()) {
			if (!displayX.readAndDispatch())
				displayX.sleep();
		}
		displayX.dispose();
	}
}
